##Sketchup::require 'iconv.so'
Sketchup::require 'win32ole.so'
Sketchup::require 'excel-17'
###require 'b_list'

##############
class Sketchup::Face
		def mats_areas

           
           ###init
                     @mat_area=[]
                       @mats_areas=Mats_areas.new
 ##puts material
##puts back_material
##puts material==back_material
###########
if material
  	
		if ( material==back_material )  
		    	   
		  		@mat_area<<material
                     		a=area*2
    ### puts "enter if"
                     		@mat_area<<a
                     		@mats_areas<<@mat_area
				 
                                
                          
               else 
                     
                     ## puts "enters else"
 			@mat_area<<material
                    	 @mat_area<<area
                    	@mats_areas<<@mat_area
#############
                 

		 	@mat_area=[]   
                       if back_material 
		  	@mat_area<<back_material
                     	@mat_area<<area
                     	@mats_areas<<@mat_area
                       end
                       ## puts @mats_areas
                
               end
        else
            if back_material 
              		@mat_area<<back_material
                     	@mat_area<<area
                     	@mats_areas<<@mat_area
         end
end 
 ##puts "not enters else"
         return @mats_areas
end

end
################

class Sketchup::Group
 def mats_areas
        @mats_areas=Mats_areas.new

          entities.each do |e|
		if (e.is_a? Sketchup::Group)
                    if (not e.mats_areas.empty? ) 
                      
			 @mats_areas.merge e.mats_areas
                    end
                   end
		if (e.is_a? Sketchup::ComponentInstance)
                    if (not e.mats_areas.empty? ) 
                      
			 @mats_areas.merge e.mats_areas
                    end
                   end
       		
		if ( e.kind_of? Sketchup::Face ) 
                      if (not e.mats_areas.empty? ) 
                      
			 @mats_areas.merge e.mats_areas
                       end
                  end
           end

	return @mats_areas		
  end 
end 

################

class Sketchup::ComponentDefinition
 def mats_areas
        @mats_areas=Mats_areas.new

          entities.each do |e|
		if (e.is_a? Sketchup::Group)
                    if (not e.mats_areas.empty? ) 
                      
			 @mats_areas.merge e.mats_areas
                    end
                   end
		if (e.is_a? Sketchup::ComponentInstance)
                    if (not e.mats_areas.empty? ) 
                      
			 @mats_areas.merge e.mats_areas
                    end
                   end
       
		if ( e.kind_of? Sketchup::Face ) 
                      if (not e.mats_areas.empty? ) 
                      
			 @mats_areas.merge e.mats_areas
                       end
                  end
           end

	return @mats_areas		
  end 
end 
###############


class Sketchup::ComponentInstance

	def mats_areas
		@mats_areas=Mats_areas.new
		if (mat=self.material )
			self.definition.entities.each {|e|
				if (e.is_a? Sketchup::Face)
					if (not e.material)

				  	 e.material=mat
					end
				end
			}
		end  
		 ### end replace face in defition with 
               #####	component material
	   return self.definition.mats_areas
	end
end
##################
###########calculate mats_areas in whole model
class Sketchup::Model

	def mats_areas
             @mats_areas=Mats_areas.new
		self.entities.each {|e|
			if (e.is_a? Sketchup::Group)
                    if (not e.mats_areas.empty? ) 
                      
			 @mats_areas.merge e.mats_areas
                    end
                   end
		  if (e.is_a? Sketchup::ComponentInstance)
			@mats_areas.merge e.mats_areas
                   end
  		if (e.is_a? Sketchup::Face)
			@mats_areas.merge e.mats_areas
                   end
}
		return @mats_areas
	end
end
##################
class Mats_areas < Array
	############# add new method merge to array class
	#########[[a,2],[b,3]].merge [[a,3]]->[[a,5],[b,3]]
def empty
	self.each {|e| 
	 e[1]=0
          }
end
 def merge(a_mats_areas)

     ##mat
       ##area
    a_mats_areas.each {|e|

     mat=e[0]
     area=e[1]
    if (temp=self.assoc mat)
         ind=self.rindex temp
	  self.delete_at ind
          area+=temp[1]
          temp.replace [mat,area]
          self<<temp 
          else
        self<<e
        end
}       
         ##end block
 return self
end
###########

class Sketchup::Model

	def mats_areas
             @mats_areas=Mats_areas.new
		## @mats_areas.empty
		self.entities.each {|e|
		  if (e.is_a? Sketchup::ComponentInstance)
			@mats_areas.merge e.mats_areas
                   end
  		if (e.is_a? Sketchup::Face)
			@mats_areas.merge e.mats_areas
                   end
}
		return @mats_areas
	end
end
#############


 ################
    def input_mats_costs
        prompts=self.collect {|e|
                e[0].display_name
                    }

	defaults=self.collect {|e|
           if (ed1=e[0].attribute_dictionary  "cost")
		if (cspsm=ed1["cost_mat_per_sqmetre"])
			cspsm
			end
             else
                0
		end
                    }
	list=self.collect {|e|
                ""
                    }

     input = UI.inputbox prompts, defaults, list, "Tell me about cost per square meter."
           ind_input=0
      self.each {|e|
            
		     e[0].set_attribute "cost","cost_mat_per_sqmetre",input[ind_input]
             
                    ind_input+=1
		}
        end


#################

def output_mats_areas_costp
	##dir=Sketchup.active_model.path
excel = Excel::WorkBook.new  
 worksheet = excel.add_worksheet("materials")  
worksheet.add_title('materials summary')  
 row = worksheet.add_row  

    row.add_cell("materials") 
    row.add_cell("areas") 
    row.add_cell("cost_per_sqmetre") 
     row = worksheet.add_row 
         row.add_cell("name") 
    row.add_cell("m2") 
    row.add_cell("yuan/m2") 
		#f.write "\n"
      #          f.write("         "+"\t"+"m2"+"\t"+"yuan/m2")
		#f.write "\n"
self.each {|e|
                  ename=e[0].display_name
		   ecost=e[0].get_attribute "cost","cost_mat_per_sqmetre"
                   ecost=ecost.to_s
 		           row = worksheet.add_row
              #  f.write(ename+"\t"+e[1].to_m.to_m.to_s+"\t"+ecost)
               row.add_cell(ename)  
               row.add_cell(e[1].to_m.to_m.to_s)  
               row.add_cell(ecost)  

                #f.write "\n"
                    }
		
       # f.close
        excel.show 
        
	UI.messagebox "output complete"
	
end


##############

end



############User Interface

if( not $Cost_menu_loaded )
	subm=UI.menu("Tools").add_submenu ("Cost")
      subm.add_item("input_material_costpersqmetre") { Sketchup.active_model.mats_areas.input_mats_costs } 
    subm.add_item("Cost") { Sketchup.active_model.mats_areas.output_mats_areas_costp } 
 #$Cost_menu_loaded =false
    $Cost_menu_loaded = true
end

################
